package com.nms.dao.impl;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.nms.dao.NewsDao;
import com.nms.model.Comment;
import com.nms.model.News;
import com.nms.model.pojo.PageParam;

public class NewsDaoImpl implements NewsDao {

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> findAll() {
		return (List<News>) this.hibernateTemplate.find("from News");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> findByPage(PageParam pageParam) {

		Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
		SQLQuery sqlQuery = s.createSQLQuery("select count(*) from t_news");
		pageParam.setTotalCount((int) ((BigInteger) sqlQuery.uniqueResult())
				.longValue());

		Query q = s.createQuery("from News as n");
		q.setFirstResult(pageParam.getIndex());
		q.setMaxResults(pageParam.getSize());
		return (List<News>) q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> findByKeyword(String keyword, PageParam pageParam) {
		Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
		SQLQuery sqlQuery = s
				.createSQLQuery("select count(*) from News where keyword='%"
						+ keyword + "%'");
		pageParam.setTotalCount((int) ((BigInteger) sqlQuery.uniqueResult())
				.longValue());

		Query q = s.createQuery("from News as n where n.keyword like '%"
				+ keyword + "%'");
		q.setFirstResult(pageParam.getIndex());
		q.setMaxResults(pageParam.getSize());
		return (List<News>) q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> findByDate(Date date, PageParam pageParam) {
		Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
		SQLQuery sqlQuery = s
				.createSQLQuery("select count(*) from News where date='%"
						+ date + "%'");
		pageParam.setTotalCount((int) ((BigInteger) sqlQuery.uniqueResult())
				.longValue());

		Query q = s.createQuery("from News as n where n.date like '%" + date
				+ "%'");
		q.setFirstResult(pageParam.getIndex());
		q.setMaxResults(pageParam.getSize());
		return (List<News>) q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> findByType(Integer type, PageParam pageParam) {
		Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
		SQLQuery sqlQuery = s
				.createSQLQuery("select count(*) from News where type=" + type
						+ "");
		pageParam.setTotalCount((int) ((BigInteger) sqlQuery.uniqueResult())
				.longValue());

		Query q = s.createQuery("from News as n where n.type=" + type + "");
		q.setFirstResult(pageParam.getIndex());
		q.setMaxResults(pageParam.getSize());
		return (List<News>) q.list();
	}

	@Override
	public News findById(String id) {
		List<?> news = hibernateTemplate.find("from News n where n.id='" + id
				+ "'");
		if (news == null || news.size() == 0)
			return null;
		return (News) news.get(0);
	}

	@Override
	public boolean deleteById(String id) {
		News n = findById(id);
		if (n == null)
			return false;
		hibernateTemplate.delete(n);
		return true;
	}

	@Override
	public boolean update(News news) {
		// News n = findById(news.getId());
		// if(n == null)
		// return false;
		// News n = new News();
		// UserLoginUtil.getInstance().copyNews(news, n);
		Set<Comment> cs = news.getComments();
		if (cs != null)
			for (Comment c : cs) {
				hibernateTemplate.saveOrUpdate(c);
			}
		hibernateTemplate.update(news);
		// Session s =
		// this.hibernateTemplate.getSessionFactory().getCurrentSession();
		// Query q = s.createQuery("update News n set n.mainTitle='" +
		// news.getMainTitle() + "'" + " where n.id='" + news.getId() + "'");
		// q.executeUpdate();
		return true;
	}

	@Override
	public Serializable add(News news) {
		News n = findById(news.getId());
		if (n != null)
			return null;
		return hibernateTemplate.save(news);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> findHomepageNews() {
		List<News> results = new ArrayList<News>();
		Session s = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		Query q =
				s.createQuery(
						"from News where topLine=1");
		q.setFirstResult(0);
		q.setMaxResults(30);
		results.addAll(q.list());
		List<Integer> nt = (List<Integer>) hibernateTemplate.find("select id from NewsType where type=3");

		for (Integer newsType : nt) {
			q = s.createQuery("from News as n where n.topLine=0 and n.type.id=1 and n.type2.id=4 and n.type3.id="
					+ newsType + "");
			q.setFirstResult(0);
			q.setMaxResults(10);
			results.addAll(q.list());
		}
		q = s.createQuery("from News as n where n.topLine=0 and n.type.id=1 and n.type2.id=5");
		q.setFirstResult(0);
		q.setMaxResults(20);
		results.addAll(q.list());
		return results;
	}

}
